package com.bwie.service.impl;

import cn.hutool.core.io.file.FileNameUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.GroupActive;
import com.bwie.service.GroupActiveService;
import com.bwie.mapper.GroupActiveMapper;
import com.bwie.utils.Result;
import com.bwie.vo.GroupActiveVo;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;

/**
* @author 陈彤彤
* @description 针对表【t_group_active(拼团活动表)】的数据库操作Service实现
* @createDate 2025-07-27 20:53:56
*/
@Service
public class GroupActiveServiceImpl extends ServiceImpl<GroupActiveMapper, GroupActive>
    implements GroupActiveService{

    @Autowired
    GroupActiveMapper groupActiveMapper;

    @Autowired
    FastFileStorageClient fileStorageClient;

    @Override
    public Result groupActiveList(GroupActiveVo groupActiveVo) {
        Page<GroupActive> groupActivePage = new Page<>(groupActiveVo.getPageNum(), groupActiveVo.getPageSize());

        Page<GroupActive> page = groupActiveMapper.groupActiveList(groupActivePage, groupActiveVo);
        return Result.ok().setData(page);
    }

    /**
    * @param groupActive:
    * @return Result
    * @author :陈彤彤
    * @description 添加活动信息
    * @date 2025/7/28 10:50
    */
    @Override
    public Result addActive(GroupActive groupActive) {
        /*校验活动名称是否存在*/
        GroupActive active = groupActiveMapper.selectOne(new QueryWrapper<GroupActive>().lambda().eq(GroupActive::getActiveName, groupActive.getActiveName()));
        if (active != null){
            return Result.error().setMsg("活动名称已存在");
        }
        /*添加信息*/
        groupActive.setCreatedBy("system");
        groupActive.setCreatedTime(new Date());
        groupActive.setUpdatedBy("system");
        groupActive.setUpdatedTime(new Date());
        groupActiveMapper.insert(groupActive);
        return Result.ok();
    }

    /**
    * @param file:
    * @return Result
    * @author :陈彤彤
    * @description 活动文件上传
    * @date 2025/7/28 16:19
    */
    @Override
    public Result uploadFile(MultipartFile file) {
        try {
            /*获取文件扩展名*/
            String extName = FileNameUtil.extName(file.getOriginalFilename());

            /*文件上传*/
            StorePath path = fileStorageClient.uploadFile(file.getInputStream(), file.getSize(), extName, null);

            /*获取文件上传路径*/
            String url = "http://192.168.80.192:8888/" + path.getFullPath();
            return Result.ok().setData(url);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error().setMsg("上传失败");
        }
    }

    /**
    * @param activeId:
    * @return Result
    * @author :陈彤彤
    * @description 删除活动信息
    * @date 2025/7/28 16:56
    */
    @Override
    public Result deleteByActiveId(Integer activeId) {
        /*判断活动信息是否存在*/
        GroupActive groupActive = groupActiveMapper.selectById(activeId);
        if (groupActive == null){
            return Result.error().setMsg("活动信息不存在");
        }

        groupActiveMapper.deleteById(activeId);
        return Result.ok();
    }

    /**
    * @param groupActive:
    * @return Result
    * @author :陈彤彤
    * @description 修改活动信息
    * @date 2025/7/28 19:08
    */
    @Override
    public Result updateActive(GroupActive groupActive) {
        GroupActive active = groupActiveMapper.selectOne(new QueryWrapper<GroupActive>().lambda().eq(GroupActive::getActiveName, groupActive.getActiveName()));
        if (active != null) {
            return Result.ok().setMsg("活动信息已存在");
        }
        groupActiveMapper.updateById(groupActive);
        return Result.ok();
    }


}




